package cn.tannn.ops.nginx.controller;

import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.exception.built.BusinessException;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.ops.common.constant.CloudEnums;
import cn.tannn.ops.nginx.controller.vo.DnsDomain;
import cn.tannn.ops.nginx.controller.vo.DnsDomainRecord;
import cn.tannn.ops.nginx.entity.CloudAccessKey;
import cn.tannn.ops.nginx.service.CloudAccessKeyService;
import cn.tannn.ops.util.AliDnsUtil;
import cn.tannn.ops.util.DnsUtil;
import cn.tannn.ops.util.HwDnsUtil;
import com.aliyun.sdk.service.alidns20150109.AsyncClient;
import com.huaweicloud.sdk.dns.v2.DnsClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 域名解析记录
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/9/20 上午10:21
 */
@PathRestController("/recordParsing/domain")
@Tag(name = "云厂商Access管理")
@Slf4j
@RequiredArgsConstructor
public class DomainParsingRecordController {
    private final CloudAccessKeyService cloudAccessKeyService;

    @GetMapping("/all/{accessId}")
    @Operation(summary = "当前access用户的域名列表")
    public ResultVO<List<DnsDomain>> domains(@PathVariable("accessId") String accessId) {
        Optional<CloudAccessKey> accessKey = cloudAccessKeyService.findOnly("accessId", accessId);
        if (accessKey.isPresent()) {
            return ResultVO.success(DnsUtil.findDomains(accessKey.get()));
        } else {
            throw new BusinessException("access不存在，请检查后重试！");
        }
    }

    @PostMapping("/records/{accessId}")
    @Operation(summary = "domain的解析记录")
    public ResultVO<List<DnsDomainRecord>> records(@PathVariable("accessId") String accessId, @RequestBody @Valid DnsDomain domain) {
        Optional<CloudAccessKey> accessKey = cloudAccessKeyService.findOnly("accessId", accessId);
        if (accessKey.isPresent()) {
            return ResultVO.success(DnsUtil.findDomainsRecord(accessKey.get(), domain));
        } else {
            throw new BusinessException("access不存在，请检查后重试！");
        }
    }

    @GetMapping("/delete/{accessId}/{recordId}/{cloud}")
    @Operation(summary = "删除解析记录")
    public ResultVO<String> deleteRecord(@PathVariable("accessId") String accessId,
                                    @PathVariable("recordId") String recordId,
                                    @RequestParam(value = "zoneId", required = false) String zoneId,
                                    @PathVariable("cloud") CloudEnums cloud) {
        Optional<CloudAccessKey> accessKey = cloudAccessKeyService.findOnly("accessId", accessId);
        if (accessKey.isPresent()) {
            CloudAccessKey cloudAccessKey = accessKey.get();
            if (cloud == CloudEnums.ALI) {
                AsyncClient ali = AliDnsUtil.init(cloudAccessKey);
                DnsUtil.deleteDomainRecord(ali, recordId);
            } else if (cloud == CloudEnums.HUAWEI) {
                DnsClient hw = HwDnsUtil.init(cloudAccessKey);
                DnsUtil.deleteDomainRecord(hw, recordId, zoneId);
            } else {
                throw new BusinessException("暂不支持云厂商" + cloud.getName());
            }
        } else {
            throw new BusinessException("access不存在，请检查后重试！");
        }
        return ResultVO.successMessage("删除成功");
    }
}
